package com.yrihr.wisdom.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yrihr.wisdom.entity.News;
import com.yrihr.wisdom.service.NewsSercice;
import com.yrihr.wisdom.util.JsoupUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : WebReptilesController
 * @Description : 爬取水利专业知识服务系统资讯
 * @Author : wind  手动爬虫
 * @Date: 2020-11-16 17:01
 * @Version 1.0
 */
@Controller
@RequiredArgsConstructor
@Slf4j
public class WebReptilesController {
    private final NewsSercice newsSercice;

    @RequestMapping("/manualCrawler")
    public void manualCrawler(){

        int pageNo = 1;
        Document doc = null;
        String url = "http://mwr.ckcest.cn/wnews/wrNewsB/getList?pageSize=1&pageNo=" + pageNo;
        try {
            doc = JsoupUtil.getConnection(url, "POST", "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject body = JSONObject.parseObject(doc.body().text());
        JSONObject list = body.getJSONObject("obj");
        int pages = (int) list.get("pages");
        if (pages > 0) {
            for (int i = 0; i < pages + 1; i++) {
                try {
                    Thread.sleep(200);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                pageNo = i;
                url = "http://mwr.ckcest.cn/wnews/wrNewsB/getList?pageSize=1&pageNo=" + pageNo;
                try {
                    doc = JsoupUtil.getConnection(url, "POST", "utf-8");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    body = JSONObject.parseObject(doc.body().text());
                    list = body.getJSONObject("obj");
                    List<Object> listNews = (List<Object>) list.get("list");
                    for (int j = 0; j < listNews.size(); j++) {
                        News news = new News();
                        Map map = (Map) listNews.get(j);
                        news.setId(map.get("id") + "");
                        news.setProCat(map.get("proCat") + "");
                        news.setNewsRsn(map.get("newsRsn") + "");
                        news.setNewsCd(map.get("newsCd") + "");
                        news.setId(map.get("id") + "");
                        news.setNewRecord((Boolean) map.get("isNewRecord"));
                        news.setDetailUrl(map.get("detailUrl") + "");
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        news.setPubDate(sdf.parse(map.get("pubDate") + ""));
                        news.setNewsNm(map.get("newsNm") + "");
                        news.setUpdateTime(sdf.parse(sdf.format(new Date())));
                        QueryWrapper<News> query = Wrappers.query();
                        query.eq("newsCd",news.getNewsCd());
                        query.eq("proCat",news.getProCat());
                        int count = newsSercice.count(query);
                        if (count == 0) {
                            newsSercice.save(news);
                        } else {
                            // 判断发布时间是否正确
                            query.ne("pubDate",news.getPubDate());
                            count = newsSercice.count(query);
                            // 如果找到id=id  其他不同，即count=1，找到了不同，则需要更新
                            // 如果找到id=id  其他全相同，即结果count=0,没有找到不同，则不更新
                            // 这两句话太绕了
                            if (count == 1) {
                                boolean b = newsSercice.saveOrUpdate(news);
                                log.info("存在：1条记录，id为：" + news.getNewsCd() + "但发布时间与网站不匹配，则需更新，即将更新！");
                                if (b) {
                                    log.info("存在：1条记录，id为：" + news.getNewsCd() + "但发布时间与网站不匹配，已更新成功！");
                                }
                            } else {
                                log.info("存在：1条记录，id为：" + news.getNewsCd() + "发布时间与网站匹配，无需更新");
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }

            }
        }

        //System.out.println(list1);

    }

}
